package com.tebiecloud.act.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.act.client.model.entity.ActGiftPack;
import com.tebiecloud.act.server.service.ActGiftPackService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 活动礼包 前端控制器
 * @author tebie
 * @date 2019-09-24
 */
@RestController
@RequestMapping("/actgiftpack")
public class ActGiftPackController {

    @Autowired
    private ActGiftPackService actGiftPackService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<ActGiftPack>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        ActGiftPack query = pageParams.mapToObject(ActGiftPack.class);
        QueryWrapper<ActGiftPack> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(actGiftPackService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<ActGiftPack> get(@RequestParam("id") Long id) {
        ActGiftPack entity = actGiftPackService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param activityId 活动id,
     * @param price 售价,
     * @param retailPrice 门市价,
     * @param costPrice 成本价,
     * @param commissionRate 佣金比例（单位：/100=百分比）,
     * @param stockQty 库存数量：-99为不限库存
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "activityId", required = false) Long activityId,
        @RequestParam(value = "price", required = false) Integer price,
        @RequestParam(value = "retailPrice", required = false) Integer retailPrice,
        @RequestParam(value = "costPrice", required = false) Integer costPrice,
        @RequestParam(value = "commissionRate", required = false) Integer commissionRate,
        @RequestParam(value = "stockQty", required = false) Integer stockQty
    ) {
        ActGiftPack entity = new ActGiftPack();
        entity.setActivityId(activityId);
        entity.setPrice(price);
        entity.setRetailPrice(retailPrice);
        entity.setCostPrice(costPrice);
        entity.setCommissionRate(commissionRate);
        entity.setStockQty(stockQty);
        actGiftPackService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param activityId 活动id
     * @param price 售价
     * @param retailPrice 门市价
     * @param costPrice 成本价
     * @param commissionRate 佣金比例（单位：/100=百分比）
     * @param stockQty 库存数量：-99为不限库存
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "actGiftId", required = false) Long actGiftId,
            @RequestParam(value = "activityId", required = false) Long activityId,
            @RequestParam(value = "price", required = false) Integer price,
            @RequestParam(value = "retailPrice", required = false) Integer retailPrice,
            @RequestParam(value = "costPrice", required = false) Integer costPrice,
            @RequestParam(value = "commissionRate", required = false) Integer commissionRate,
            @RequestParam(value = "stockQty", required = false) Integer stockQty
    ) {
        ActGiftPack entity = new ActGiftPack();
        entity.setActGiftId(actGiftId);
        entity.setActivityId(activityId);
        entity.setPrice(price);
        entity.setRetailPrice(retailPrice);
        entity.setCostPrice(costPrice);
        entity.setCommissionRate(commissionRate);
        entity.setStockQty(stockQty);
        actGiftPackService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            actGiftPackService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            actGiftPackService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
